<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/04 9:44
 * Desc: uniapp提问
 */
class Model_Api_V2_Question
{

    /**
     * 问答搜索
     * @param $params
     * @return array
     */
    public static function search($params = array())
    {
        /**
         * @var $pagesize
         * @var $page
         * @var $ques_type
         * @var $article_id
         * @var $type_id
         * @var $mid
         * @var $keyword
         */
        extract($params);

        $builder = DB::select()
            ->from('question');

        if (isset($params['ques_type'])) {
            $builder->where('questype', '=', $ques_type);
            // 问题类型
            if ($ques_type == 0) {
                if (!empty($article_id)) {
                    $builder->where('productid', '=', $article_id);
                }
                if (!empty($type_id)) {
                    $builder->where('typeid', '=', $type_id);
                }
            }
        }
        if (!empty($mid)) {
            $builder->where('memberid', '=', $mid);
        } else {
            // 不是会员的问答只查看有回复的
            $builder->where('replytime', '!=', '');
        }
        if (!empty($keyword)) {
            $builder->where('content', 'LIKE', "%{$keyword}%");
        }
        $total = $builder->execute()->count();
        if (!empty($mid)) {
            $builder->order_by('addtime', 'desc');
        } else {
            $builder->order_by('replytime', 'desc');
        }

        $offset = St_Page::offset($page, $pagesize);
        $list   = $builder->offset($offset)->limit($pagesize)->execute()->as_array();

        return array(
            'total' => $total,
            'list' => $list
        );
    }


    /**
     * @function 获取产品信息
     * @param $typeid
     * @param $productid
     * @return array
     */
    public static function get_product_info($typeid, $productid)
    {
        $out = array();
        if ($typeid) {
            $model = ORM::factory('model', $typeid);
            $table  = $model->maintable;
            $pinyin = !empty($model->correct) ? $model->correct : $model->pinyin;
            if ($table) {
                $info         = ORM::factory($table, $productid)->as_array();
                $url          = Common::get_web_url($info['webid']) . "/{$pinyin}/show_{$info['aid']}.html";
                $out['title'] = $info['title'];
                $out['url']   = $url;
            }
        }
        return $out;
    }
}